package com.example.test1.controller;

import com.example.test1.model.Student;
import com.example.test1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public List<Student> getAllUsers(){
        return studentService.list();
    }

    @GetMapping("/student/{id}")
    public Student geetUserById(@PathVariable("id") Integer id){
        Student student=studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public void addUser(@RequestBody Student student) {
        studentService.save(student);
    }

    @PutMapping("/student")
    public String updateUser(@RequestBody Student student) {
        if (studentService.updateById(student)) {
            return "更新用户成功！";
        } else return "更新用户失败！";
    }

    @DeleteMapping("/student")
    public String deleteUserById(Integer id){
        if (studentService.removeById(id)){
            return "删除用户成功！";
        }
        else return "删除用户失败！";
    }
}
